﻿IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'InsertPermiso')
	BEGIN
		DROP  Procedure  InsertPermiso
	END

GO

CREATE Procedure InsertPermiso
	@estado bit,
	@idEmpleado smallint,
	@idFormulario tinyint,
	@tipoPermiso tinyint
AS
BEGIN
	SET NOCOUNT ON
	
	BEGIN TRY
	
	DECLARE @existe int
	SET @existe = ISNULL((SELECT COUNT(Estado) FROM Permiso WHERE IdFormulario = @idFormulario AND idEmpleado = @idEmpleado AND TipoPermiso = @tipoPermiso), 0)
		
	IF @existe = 0
	BEGIN
		INSERT INTO Permiso (Estado, IdEmpleado, IdFormulario, TipoPermiso)
		VALUES (@estado, @idEmpleado, @idFormulario, @tipoPermiso)
    END
    ELSE
    BEGIN
		UPDATE Permiso SET Estado = @estado WHERE IdFormulario = @idFormulario AND idEmpleado = @idEmpleado AND TipoPermiso = @tipoPermiso
    END
    
    END TRY

    BEGIN CATCH
		EXEC RethrowError;
	END CATCH
    
    SET NOCOUNT OFF
END    

GO

GRANT EXEC ON InsertPermiso TO PUBLIC

GO


